/**
 * @file:          motor_drv42_v12.c
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.10.25
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.08.14,11:14:16
 */

/* Include Files **************************************************************/
#include "motor_drv42_v12.h"

/* Global Variable Define *****************************************************/

/* Function Define ************************************************************/
uint8 Motor_drv42_v12_Init(struct Motor_drv42 *self)
{
    self->LedToggle = Motor_drv42_v12_LedToggle;
    // self->AdcCalibration = Motor_drv42_v12_AdcCalibration;
    self->Adc2Current = Motor_drv42_v12_Adc2Current;
    self->GetIaFdb = Motor_drv42_v12_GetIaFdb;
    self->GetIbFdb = Motor_drv42_v12_GetIbFdb;
    self->UpdatePWM = Motor_drv42_v12_UpdatePWM;
    self->TestGpio_Set = Motor_drv42_v12_TestGpio_Set;
    self->TestGpio_Reset = Motor_drv42_v12_TestGpio_Reset;
    self->GetPowerVoltage = Motor_drv42_v12_GetPowerVoltage;
    self->GetMcuTemperature = Motor_drv42_v12_GetMcuTemperature;

    self->led_toggle_period_count = (uint32)(self->param_led_toggle_period_s / 2 / self->mcu->param_task_period_s);
    self->pin_EN_value = 1;
    self->pin_EN_value_last = 1;

    // self->mcu->GpioToggle(self->param_pin_LED_in.pin);
    // printf("asdasd 12\r\n");

    // self->mcu->SetPwm(self->mcu, 5, 0.5);
    // self->mcu->SetPwm(self->mcu, 6, 0.5);

    return TRUE;
}

void Motor_drv42_v12_LedToggle(struct Motor_drv42 *self)
{
    // printf("asd\r\n");
    // self->mcu->GpioToggle(self->param_pin_LED_in);

    // if (self->GetIaFdb(self) > 0)
    // {
    // DBprintf("ll:%d\r\n", self->led_toggle_period_count);
    // DBprintf("l:%d\r\n", self->led_toggle_count);
    // DBprintf("l:%d\r\n", self->param_pin_LED_in.pin);
    if (self->led_toggle_count % self->led_toggle_period_count == 0)
    {
        // DBprintf("----:%d\r\n", self->led_toggle_count);
        self->mcu->GpioToggle(self->param_pin_LED.pin);
    }
    self->led_toggle_count++;
    // }
    // else if (self->GetIaFdb(self) < 0)
    // {
    //     self->mcu->GpioSet(self->param_pin_LED_in);
    //     self->mcu->DelayMs(self->mcu, 250);
    //     self->mcu->DelayMs(self->mcu, 250);
    //     self->mcu->DelayMs(self->mcu, 250);
    //     self->mcu->DelayMs(self->mcu, 250);

    //     // self->mcu->GpioToggle(self->param_pin_LED_in);
    //     self->mcu->GpioReset(self->param_pin_LED_in);
    //     self->mcu->DelayMs(self->mcu, 250);
    //     self->mcu->DelayMs(self->mcu, 250);
    //     self->mcu->DelayMs(self->mcu, 250);
    //     self->mcu->DelayMs(self->mcu, 250);
    // }
    // else
    // {
    // }
}

uint8 Motor_drv42_v12_AdcCalibration(struct Motor_drv42 *self)
{
    uint16 i = 0;
    int32 Ia_sum = 0;
    int32 Ib_sum = 0;

    for (i = 0; i < self->param_adc_calibration_buffer_size; i++)
    {
        self->mcu->ADC_StartConversion();
        self->mcu->ADC_WaitConversionComplete();
        Ia_sum += self->mcu->AdcGetResult(ADC_NUM1, 1);
        Ib_sum += self->mcu->AdcGetResult(ADC_NUM2, 1);
    }

    self->param_Ia_adc_offset = Ia_sum / self->param_adc_calibration_buffer_size; // - self->mcu->adc1_offset;
    self->param_Ib_adc_offset = Ib_sum / self->param_adc_calibration_buffer_size; // - self->mcu->adc2_offset;

    return 1;
}

float32 Motor_drv42_v12_Adc2Current(struct Motor_drv42 *self, int16 adc_val)
{
    return (float32)(adc_val) / 4096 * self->param_adc_ref_V / self->param_amp_gain / self->param_R_of_current_sample_ohm;
    // return (float32)(adc_val) / 4096 * self->param_adc_ref_V;
}

float32 Motor_drv42_v12_GetIaFdb(struct Motor_drv42 *self)
{
    float32 out = 0;

    self->Ia_adc_value = self->mcu->AdcGetResult(ADC_NUM1, 1);
    out = self->param_current_dir * self->Adc2Current(self, self->Ia_adc_value - self->param_Ia_adc_offset);

    return out;
}

float32 Motor_drv42_v12_GetIbFdb(struct Motor_drv42 *self)
{
    float32 out = 0;

    self->Ib_adc_value = self->mcu->AdcGetResult(ADC_NUM2, 1);
    out = self->param_current_dir * self->Adc2Current(self, self->Ib_adc_value - self->param_Ib_adc_offset);

    return out;
}

void Motor_drv42_v12_UpdatePWM(struct Motor_drv42 *self)
{
    if (self->param_pwm_output_en == TRUE)
    {
        self->mcu->SetAllPwm(self->mcu, self->in_pwm_duty_A1, self->in_pwm_duty_A2, self->in_pwm_duty_B1, self->in_pwm_duty_B2);
    }
    else
    {
        self->mcu->SetAllPwm(self->mcu, 0, 0, 0, 0);
    }
}

void Motor_drv42_v12_TestGpio_Set(struct Motor_drv42 *self)
{
    self->mcu->GpioSet(self->param_pin_TestGpio.pin);
}

void Motor_drv42_v12_TestGpio_Reset(struct Motor_drv42 *self)
{
    self->mcu->GpioReset(self->param_pin_TestGpio.pin);
}

float Motor_drv42_v12_GetPowerVoltage(struct Motor_drv42 *self)
{
    self->board_detect_power_supply_V = self->mcu->AdcGetResult(ADC_NUM2, 2) / 4096.0 * self->param_adc_ref_V * self->param_power_voltage_sample_gain;
    return self->board_detect_power_supply_V;
}

float Motor_drv42_v12_GetMcuTemperature(struct Motor_drv42 *self)
{
    int32_t Refer_Volt_mV, Refer_Temper_C;

    Refer_Volt_mV = MCU_REFER_VOLT_MV();
    Refer_Temper_C = MCU_REFER_TEMPER_C();
    // printf("Refer_Volt_V:%d, Refer_Temper_C: %d\r\n", Refer_Volt_mV, Refer_Temper_C);

    // return (float)TempSensor_Volt_To_Temper(self->mcu->AdcGetResult(ADC_NUM1, 2) * 3300 / 4096);
    self->board_detect_temperature_C = Refer_Temper_C - (self->mcu->AdcGetResult(ADC_NUM1, 2) / 4096.0F * self->param_adc_ref_V * 1000.0F - Refer_Volt_mV) / ADC_TEMP_AVG_SLOPE;
    return self->board_detect_temperature_C;
}
